package com.mango.ch01;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import com.mango.HDFSTools.HDFSAPI;

/**
 * 传统的二次排序方式
 * 
 * @author Mango
 *
 */
public class TemperatureJob {
	/*static File outPath = new File("D:/HadoopData/TemperatureJob/output");
	static Path inputPath = new Path("hdfs://hdf01.cn:9000/TemperatureJob/input");*/
	static File outPath = new File("D:/HadoopData/TemperatureJob/output");
	static Path inputPath = new Path("D:/HadoopData/input");

	public static void main(String[] args)
			throws IOException, ClassNotFoundException, InterruptedException, URISyntaxException {
		Configuration conf = new Configuration();
		// 检查输入输出路径
//		new HDFSAPI(conf).CheckPath(new Path[] { inputPath }, new File[] { outPath });
		Job job = Job.getInstance(conf);
		job.setJarByClass(TemperatureJob.class);
		job.setJobName("TemperatureJob");

		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);
		job.setMapOutputKeyClass(DateTemperaturePair.class);
		job.setMapOutputValueClass(IntWritable.class);

		job.setNumReduceTasks(2);

		job.setMapperClass(TemperatureMapper.class);
		job.setReducerClass(TemperatureReducer.class);

		job.setPartitionerClass(DateTemperaturePartitioner.class);
		job.setGroupingComparatorClass(DateTemperatureGroupingComparator.class);

		FileInputFormat.setInputPaths(job, inputPath);
		FileOutputFormat.setOutputPath(job, new Path(outPath.toString()));

		boolean status = job.waitForCompletion(true);
		System.out.println("job run over");
		System.exit(status ? 0 : 1);
	}
}
